package com.dayu.finecomm.aop.adapt.mongo;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

/**
 * AopMongoAdapt 数据操作
 * 
 * @author XuJialiang
 * @since 2017-11-10 14:45:12
 * @version V1.0.0
 */
@Service
public class AopMongoAdapt implements InitializingBean {

	@Resource
	AopMongoDAO dao;

	/**
	 * 插入记录
	 * 
	 * @param record
	 */
	public void insert(AopFields record) {
		dao.aop.insert(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param id
	 * @param record
	 */
	public void update(String id, AopFields record) {
		dao.aop.update(new BasicDBObject(AopFields.FIELD_ID, id), record);
	}

	/**
	 * 删除一条记录
	 * 
	 * @param id
	 */
	public void delete(String id) {
		if (null != id && false == id.isEmpty()) {
			dao.aop.remove(new BasicDBObject(AopFields.FIELD_ID, id));
		}
	}

	/**
	 * 查询一条记录
	 * 
	 * @param id
	 * @return
	 */
	public AopFields query(String id) {
		AopFields record = null;

		try {
			DBCursor cursor = dao.aop.find(new BasicDBObject(
					AopFields.FIELD_ID, id));

			List<DBObject> dbArray = cursor.toArray();

			record = new AopFields();

			record.setId((dbArray.get(0).get(AopFields.FIELD_ID).toString()));
			record.setLevel((dbArray.get(0).get(AopFields.FIELD_LEVEL)
					.toString()));
			record.setName((dbArray.get(0).get(AopFields.FIELD_NAME).toString()));
			record.setPath((dbArray.get(0).get(AopFields.FIELD_PATH).toString()));
			record.setArgs((dbArray.get(0).get(AopFields.FIELD_ARGS).toString()));
			record.setResult((dbArray.get(0).get(AopFields.FIELD_RESULT)
					.toString()));
			record.setReqTime(Long.parseLong(dbArray.get(0)
					.get(AopFields.FIELD_REQTIME).toString()));
			record.setRspTime(Long.parseLong(dbArray.get(0)
					.get(AopFields.FIELD_RSPTIME).toString()));
			record.setConsume(Long.parseLong(dbArray.get(0)
					.get(AopFields.FIELD_CONSUME).toString()));
		} catch (Exception e) {
			record = null;
		}

		return record;
	}

	public List<AopFields> querys(AopQueryItem cond) {
		List<AopFields> list = null;

		try {
			BasicDBObject query = new BasicDBObject();

			if (null != cond.level && !cond.level.isEmpty()) {
				query.put(AopFields.FIELD_LEVEL, cond.level);
			}

			if (null != cond.path && !cond.path.isEmpty()) {
				query.put(AopFields.FIELD_PATH, cond.path);
			}

			if (null != cond.name && !cond.name.isEmpty()) {
				query.put(AopFields.FIELD_NAME, cond.name);
			}

			if (0 < cond.startTime) {
				query.put(AopFields.FIELD_REQTIME, new BasicDBObject("$gte",
						cond.startTime));
			}

			if (0 < cond.endTime) {
				query.put(AopFields.FIELD_REQTIME, new BasicDBObject("$lte",
						cond.endTime));
			}

			if (0 < cond.consume) {
				query.put(AopFields.FIELD_CONSUME, new BasicDBObject("$gte",
						cond.consume));
			}

			DBCursor cursor = dao.aop.find(query);

			List<DBObject> dbArray = cursor.toArray();

			list = new ArrayList<AopFields>(dbArray.size());

			for (int i = 0; i < dbArray.size(); i++) {
				AopFields record = new AopFields();

				record.setId((dbArray.get(i).get(AopFields.FIELD_ID).toString()));
				record.setLevel((dbArray.get(i).get(AopFields.FIELD_LEVEL)
						.toString()));
				record.setName((dbArray.get(i).get(AopFields.FIELD_NAME)
						.toString()));
				record.setPath((dbArray.get(i).get(AopFields.FIELD_PATH)
						.toString()));
				record.setArgs((dbArray.get(i).get(AopFields.FIELD_ARGS)
						.toString()));
				record.setResult((dbArray.get(i).get(AopFields.FIELD_RESULT)
						.toString()));
				record.setReqTime(Long.parseLong(dbArray.get(i)
						.get(AopFields.FIELD_REQTIME).toString()));
				record.setRspTime(Long.parseLong(dbArray.get(i)
						.get(AopFields.FIELD_RSPTIME).toString()));
				record.setConsume(Long.parseLong(dbArray.get(i)
						.get(AopFields.FIELD_CONSUME).toString()));

				list.add(record);
			}

		} catch (Exception e) {
			list = null;
		}

		return list;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
	}
}
